package filters;

import jakarta.servlet.Filter;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.core.Ordered;

@SpringBootConfiguration
public class FiltersConfig {

    /**
     * 预检查过滤器（这个最先进入）
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean<Filter> optionsFilterRegistration() {
        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new OptionsFilter());
        registrationBean.addUrlPatterns("/*");
        // 设置过滤器的执行顺序
        registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registrationBean;
    }

    /**
     * 跨域过滤器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean<Filter> corsFilterRegistration() {
        FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new CorsFilter());
        registrationBean.addUrlPatterns("/*");
        // 设置过滤器的执行顺序
        registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE+1);
        return registrationBean;
    }


}
